package com.thb.pay.payment.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.thb.pay.api.payment.PayInterfaceServiceApi;
import com.thb.pay.api.payment.vo.req.PayInterfacePageReq;
import com.thb.pay.api.payment.vo.req.PayInterfaceReq;
import com.thb.pay.api.payment.vo.resp.PayInterfaceVo;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.payment.entity.PayInterface;
import com.thb.pay.payment.service.PayInterfaceService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 支付-接口表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@RestController
public class PayInterfaceController implements PayInterfaceServiceApi {

    @Autowired
    private PayInterfaceService payInterfaceService;

    @Override
    public PageResult<PayInterfaceVo> findPage(@RequestBody PayInterfacePageReq pageReq) {
        IPage<PayInterface> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<PayInterface> pageResult = payInterfaceService.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, PayInterfaceVo.class);
    }

    @Override
    public void add(@RequestBody PayInterfaceReq payInterfaceReq) {
        PayInterfaceVo payInterfaceVo = getByCode(payInterfaceReq.getCode());
        if (payInterfaceVo != null) {
            throw RRException.build("payInterface.code.exist");
        }
        PayInterface payInterface = new PayInterface();
        BeanUtil.copyProperties(payInterfaceReq, payInterface);
        payInterfaceService.insert(payInterface);
    }

    @Override
    public void update(@RequestBody PayInterfaceReq payInterfaceReq) {
        PayInterface payInterface = payInterfaceService.getById(payInterfaceReq.getId());
        if (payInterface == null) {
            throw RRException.build("payInterface.not.exist");
        }
        if (!payInterface.getCode().equals(payInterfaceReq.getCode())) {
            PayInterfaceVo payInterfaceVo = getByCode(payInterfaceReq.getCode());
            if (payInterfaceVo != null) {
                throw RRException.build("payInterface.code.exist");
            }
        }
        PayInterface targetPayInterface = new PayInterface();
        BeanUtil.copyProperties(payInterfaceReq, targetPayInterface);
        payInterfaceService.updateById(targetPayInterface);
    }

    @Override
    public PayInterfaceVo getByCode(@RequestParam("code") String code) {
        LambdaQueryWrapper<PayInterface> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PayInterface::getCode, code);
        List<PayInterface> payInterfaceList = payInterfaceService.selectList(wrapper);
        if (CollUtil.isNotEmpty(payInterfaceList)) {
            PayInterface payInterface = payInterfaceList.get(0);
            PayInterfaceVo resultData = new PayInterfaceVo();
            BeanUtil.copyProperties(payInterface, resultData);
            return resultData;
        }
        return null;
    }

    @Override
    public PayInterfaceVo getById(@RequestParam("id") Long id) {
        PayInterface payInterface = payInterfaceService.selectById(id);
        if (payInterface != null) {
            PayInterfaceVo resultData = new PayInterfaceVo();
            BeanUtil.copyProperties(payInterface, resultData);
            return resultData;
        }
        return null;
    }
}
